package cn.yougou.controller;

import cn.yougou.common.DtoUtil;
import cn.yougou.dto.Dto;
import cn.yougou.pojo.YgOrder;
import cn.yougou.service.shopcart.YgCommentService;
import cn.yougou.vo.YgCommentVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * (YgComment)表控制层
 *
 * @author 白杰崇
 * @since 2020-12-13 10:50:15
 */
@RestController
@RequestMapping("ygComment")
public class YgCommentController {

    /**
     * 服务对象
     */
    @Reference
    private YgCommentService ygCommentService;

    /**
     * 发表评论接口
     * @param ygCommentVo
     */
    @PostMapping("/insertComment")
    public Dto insertComment(@RequestBody YgCommentVo ygCommentVo){
        Dto dto = null;
        //判断对象是否为空
        if(ygCommentVo.getUserStaid() == null || ygCommentVo.getStar() == null){
            return DtoUtil.returnFail("缺少必要的参数","50001");
        }
        try {
            int config = ygCommentService.insComment(ygCommentVo);
            if(config > 0){
                dto = DtoUtil.returnSuccess("发表成功",config);
            }else{
                dto = DtoUtil.returnSuccess("发表失败","50002");
            }
        }catch (Exception e){
            e.printStackTrace();
            dto = DtoUtil.returnFail("系统错误!","49999");
        }
        return dto;
    }

    /**
     * 查询全部评论， 接收参数商品id
     * @param commodityId
     * @return
     */
    @GetMapping("commentAll")
    public Dto commentAll(Integer commodityId){
        Dto dto = null;
        if(commodityId == null){
            return DtoUtil.returnFail("缺少必要的参数","50001");
        }
        try {
            YgOrder ygOrder = new YgOrder();
            ygOrder.setCommodityid(commodityId);
            List<YgCommentVo> commentList = ygCommentService.getCommentAll(ygOrder);
            dto = DtoUtil.returnSuccess("查询成功",commentList);
        }catch (Exception e) {
            e.printStackTrace();
            dto = DtoUtil.returnFail("系统错误！","49999");
        }
        return dto;
    }

    /**
     * 获取最新一条的评论
     * @param commodityId
     * @return
     */
    @GetMapping("commentNew")
    public Dto commentNew(Integer commodityId){
        Dto dto = null;
        if(commodityId == null){
            return DtoUtil.returnFail("缺少必要的参数","50001");
        }
        try {
            YgCommentVo ygCommentVo = ygCommentService.commentNew(commodityId);
            dto = DtoUtil.returnSuccess("查询成功",ygCommentVo);
        }catch (Exception e) {
            e.printStackTrace();
            dto = DtoUtil.returnFail("系统错误！","49999");
        }
        return dto;
    }



}